package com.zdsoft.blp.zongbuju.wode.activity;

import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.zongbuju.wode.adapter.TiShiYinAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by pc on 2016/9/2.
 */
public class XZeLingSActivity extends BaseActivity {
    private ListView lv;
    private TiShiYinAdapter adapter;
    private List<Map<String,Object>> list;
    private RingtoneManager manager;
    private LinearLayout ll_back;
    @Override
    protected void init() {
        setContentView(R.layout.wode_xzlings);

        lv = find(R.id.lv);
        ll_back = find(R.id.ll_back);
        ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        manager = new RingtoneManager(this);
        // 设置RingtoneManager对象的类型为TYPE_NOTIFICATION，这样只会获取到notification的对应内容
        manager.setType(RingtoneManager.TYPE_NOTIFICATION);
        Cursor cursor = manager.getCursor();
        int num = cursor.getCount();
        Log.i("tag", num + "消息音个数");
        // 存储消息音名字的arrayList
        ArrayList<String> ringtoneList = new ArrayList<String>();
        list = new ArrayList<>();
        Map<String,Object> m = new HashMap<>();
        m.put("title","跟随系统");
        list.add(m);
        for (int i = 1; i < num; i++) {
            //获取当前i的铃声信息
            Ringtone ringtone = manager.getRingtone(i);
            //获取当前i的uri，设置notification的自定义铃声要用到
            Uri uri = manager.getRingtoneUri(i);
            //获取到当前铃声的名字
            String title = ringtone.getTitle(this);
            ringtoneList.add(title);
            Map<String ,Object> map = new HashMap<>();
            map.put("title",title);
            map.put("uri",uri);
            list.add(map);
        }
        adapter =new TiShiYinAdapter(list,getApplicationContext());
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.change(position);
                if(position==0){
                    Uri defalutUri = manager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                    Ringtone defalutRingtone = manager.getRingtone(getApplicationContext(), defalutUri);
                    defalutRingtone.play();
                }else {
                    RingtoneManager.getRingtone(getApplicationContext(),(Uri) list.get(position).get("uri")).play();
                }
            }
        });
    }
}
